package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.AgreeGuestListAdapter;
import com.jiayang.hhotel.bean.AgreeGuestModel;
import com.jiayang.hhotel.presenter.AgreeGuestPresenter;
import com.jiayang.hhotel.util.ToastUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

import cn.droidlover.xdroidmvp.custom.TitleListener;
import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/26 0026.
 * 协议客户页面
 */

public class AgreeGuestActivity extends XActivity<AgreeGuestPresenter> implements TitleListener, AgreeGuestListAdapter.btClickListener {

    private SwipeMenuRecyclerView recyclerView;
    private SwipeRefreshLayout fresh;

    private LinearLayoutManager mLinearLayoutManager;

    private int lastVisibleItem;

    private AgreeGuestListAdapter adapter;

    private int page;

    @Override
    public void initData(Bundle savedInstanceState) {
        initTitle(this,true,getString(R.string.agree_guest),R.color.orange_color,getString(R.string.transfer),this);

        fresh= (SwipeRefreshLayout) findViewById(R.id.fresh);
        recyclerView= (SwipeMenuRecyclerView) findViewById(R.id.recyclerView);
        mLinearLayoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLinearLayoutManager);

        isFresh(true);
        getP().asyncLoad();

        fresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page=1;
                getP().asyncLoad();
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                if(!recyclerView.canScrollVertically(1)){
//                    LogUtil.showLog("isScroll");
//                }
                lastVisibleItem = mLinearLayoutManager.findLastVisibleItemPosition();
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(adapter!=null){
                    if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibleItem+1==adapter.getItemCount()){
                        //todo 请求数据
                        page++;
                        ToastUtil.Toast("开始加载更多",AgreeGuestActivity.this);
                    }
                }
            }
        });
    }

    public void setAdapter(List<AgreeGuestModel> models){
        isFresh(false);
        if(adapter==null){
            adapter=new AgreeGuestListAdapter(this,models,this);
            recyclerView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
    }

    public AgreeGuestListAdapter getAdapter(){
        return adapter;
    }

    private void isFresh(final boolean is){
        fresh.post(new Runnable() {
            @Override
            public void run() {
                fresh.setRefreshing(is);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_agree_guest;
    }

    @Override
    public AgreeGuestPresenter newP() {
        return new AgreeGuestPresenter();
    }

    @Override
    public void onBTClickListener(AgreeGuestModel model, int id, int position) {
        switch (id){
            case R.id.bt_editor://编辑
                getP().transferDialog(this);
                break;
            case R.id.bt_miss://丢失
                getP().createMissDialog(this,position);
                break;
            case R.id.bt_see_details://查看详情
                Intent details=new Intent(this,AgreeGuestDetailsActivity.class);
                startActivity(details);
                break;
        }
    }


    @Override
    public void rightClickListener() {

    }

}
